"""demo Construct selector"""
from scrapy.http import HtmlResponse
from scrapy.selector import Selector
"""
<html>
 <head>
  <base href='http://example.com/' />
  <title>Example website</title>
 </head>
 <body>
  <div id='images'>
   <a href='image1.html'>Name: My image 1 <br /><img src='image1_thumb.jpg' /></a>
   <a href='image2.html'>Name: My image 2 <br /><img src='image2_thumb.jpg' /></a>
   <a href='image3.html'>Name: My image 3 <br /><img src='image3_thumb.jpg' /></a>
   <a href='image4.html'>Name: My image 4 <br /><img src='image4_thumb.jpg' /></a>
   <a href='image5.html'>Name: My image 5 <br /><img src='image5_thumb.jpg' /></a>
  </div>
 </body>
</html>
"""

"""
通过css选择器提取数据
to select text nodes, use ::text
to select attribute values, use ::attr(name) where name is the name of the attribute that you want the value of
"""

'''
//node[1] selects all the nodes occurring first under their respective parents
(//node)[1] selects all the nodes in the document, and then gets only the first of them.

from scrapy import Selector
>>> sel = Selector(text="""
     <ul class="list">
         <li>1</li>
         <li>2</li>
         <li>3</li>
     </ul>
     <ul class="list">
         <li>4</li>
         <li>5</li>
         <li>6</li>
     </ul>""")
>>> xp = lambda x: sel.xpath(x).getall()


xp("//li[1]")
['<li>1</li>', '<li>4</li>']

xp("(//li)[1]")
['<li>1</li>']
'''

"""
sel = Selector(text='<a href="#">Click here to go to the <strong>Next Page</strong></a>')

sel.xpath('//a//text()').getall() # take a peek at the node-set
['Click here to go to the ', 'Next Page']
>>> sel.xpath("string(//a[1]//text())").getall() # convert it to string
['Click here to go to the ']

sel.xpath("//a[1]").getall() # select the first node
['<a href="#">Click here to go to the <strong>Next Page</strong></a>']
>>> sel.xpath("string(//a[1])").getall() # convert it to string
['Click here to go to the Next Page']
"""


class DemoSelector:
    """demo """
    # < base href = "http://example.com/" >
    # response.xpath('//base/@href').get()
    # response.css('base::attr(href)').get()

    # response.xpath('//a[contains(@href, "image")]/@href').getall()
    # response.css('a[href*=image]::attr(href)').getall()
    """
    ['image1.html',
     'image2.html',
     'image3.html',
     'image4.html',
     'image5.html']
     
    """

    # response.xpath('//a[contains(@href, "image")]/img/@src').getall()
    # response.css('a[href*=image] img::attr(src)').getall()
    # ['image1_thumb.jpg',
    #  'image2_thumb.jpg',
    #  'image3_thumb.jpg',
    #  'image4_thumb.jpg',
    #  'image5_thumb.jpg']

    response = HtmlResponse(url='http://www.exapmle')
    Selector(response=response).xpath("//span/text()").get()
